package com.example.smart.base.ui.activity

import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.annotation.ColorInt
import androidx.viewbinding.ViewBinding
import com.example.lib.widget.AppBar

/**
 *  基于viewbinding的activity基类视图
 */
abstract class BaseTitleBarCameraActivity<V : ViewBinding> : BaseCameraActivity<V>() {

    private lateinit var headerBar: AppBar

    /**
     * 创建头部视图
     * @return 头部视图
     */
    private fun createHeaderView(): View {
        headerBar = AppBar(this)
        headerBar.setOnCloseClick(click = {
            finish()
        })
        return headerBar
    }

    override fun beforeCreateView(view: View): View {
        if (needTitle().not()) return view
        //头部视图
        val headerView = createHeaderView()
        //佈局配置
        val linearLayout = LinearLayout(this)
        linearLayout.orientation = LinearLayout.VERTICAL
        val headerLp =
            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        linearLayout.addView(headerView, headerLp)
        val contentLp =
            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
        removeParent(view)
        linearLayout.addView(view, contentLp)
        linearLayout.fitsSystemWindows = true
        return linearLayout
    }

    private fun removeParent(view: View) {
        if (view.parent is ViewGroup) {
            (view.parent as ViewGroup).removeView(view)
        }
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    override fun setTitle(title: CharSequence?) {
        if (needTitle()) headerBar.setTitle(title)
    }

    override fun setTitle(titleId: Int) {
        if (needTitle()) headerBar.setTitle(getString(titleId))
    }

    /**
     * 设置标题颜色
     *
     * @param color 标题颜色
     */
    override fun setTitleColor(@ColorInt color: Int) {
        if (needTitle()) headerBar.setTitleColor(color)
    }

    /**
     * 设置title颜色
     */
    fun setAppBarColor(@ColorInt color: Int) {
        if (needTitle()) headerBar.setAppBarColor(color)
    }

    /**
     * 设置右边文字和样式
     */
    fun setRightTextAndStyle(text: String, color: Int, click: () -> Unit = {}) {
        if (needTitle()) headerBar.setRightTextAndStyle(text, color, click)
    }

    /**
     * 隐藏右边布局
     */
    fun hideRight() {
        if (needTitle()) headerBar.hideRight()
    }

    /**
     * 设置右边图片
     */
    fun setRightImageAndClick(image: Int, click: () -> Unit = {}) {
        if (needTitle()) headerBar.setRightImageAndClick(image, click)
    }

    /**
     * 设置右边文字
     */
    fun setRightText(text: String) {
        if (needTitle()) headerBar.setRightText(text)
    }

    fun setOnCloseClick(image: Int = 0, click: () -> Unit = { finish() }) {
        if (needTitle()) headerBar.setOnCloseClick(image, click)
    }

    fun hideBottomLineView() {
        if (needTitle()) headerBar.hideBottomLineView()
    }

    open fun needTitle() = true

}